package com.ruoyi.web.controller.business;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.GongYingShangUser;
import com.ruoyi.common.enums.TuiHuoStatus;
import com.ruoyi.common.pojo.SheetData;
import com.ruoyi.common.utils.file.ExcelExporter;
import com.ruoyi.system.domain.DangKou;
import com.ruoyi.system.domain.vo.DangKouTongJiInfo;
import com.ruoyi.system.domain.vo.DongKouTongJiDetail;
import com.ruoyi.system.domain.vo.GongYingShangTongJiDetail;
import com.ruoyi.system.domain.vo.GongYingShangTongJiInfo;
import com.ruoyi.system.service.IDangkouService;
import com.ruoyi.system.service.IGongYingShangUserService;
import com.ruoyi.system.service.ITongJiService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qbf
 */

@RestController
@RequestMapping("/system/gongYingShangTongJi")
public class GongYingShangTongJiController {

    @Resource
    private ITongJiService tongJiService;

    @Resource
    private IGongYingShangUserService gongYingShangUserService;


    @GetMapping("/tongJi")
    public AjaxResult tongJi(@RequestParam("id") Long id,
                                    @RequestParam("startDay") String startDay,
                                    @RequestParam("endDay") String endDay){

        List<GongYingShangTongJiDetail> tongJiDetails = getTongJiDetail(id, startDay, endDay);
        GongYingShangTongJiInfo tongJiInfo = getTongJiInfo(id, startDay, endDay, tongJiDetails);

        Map<String, Object> data = new HashMap<>(8);
        data.put("details", tongJiDetails);
        data.put("tongJi", tongJiInfo);

        return AjaxResult.success(data);
    }

    private GongYingShangTongJiInfo getTongJiInfo(Long id, String startDay, String endDay, List<GongYingShangTongJiDetail> tongJiDetails) {
        GongYingShangTongJiInfo tongJiInfo = new GongYingShangTongJiInfo();
        tongJiInfo.setGongYingShangId(id);
        tongJiInfo.setStartDay(startDay);
        tongJiInfo.setEndDay(endDay);

        GongYingShangUser gongYingShangUser = gongYingShangUserService.getById(id);
        tongJiInfo.setGongYingShangName(gongYingShangUser.getUserName());
        BigDecimal money = new BigDecimal(0);
        BigDecimal tuiHuoMoney = new BigDecimal(0);
        for(GongYingShangTongJiDetail detail : tongJiDetails) {
            money = money.add(new BigDecimal(detail.getRuKuZongJia()));
            if(TuiHuoStatus.ROLLBACK.getCode().equals(detail.getTuiHuo())){
                tuiHuoMoney = tuiHuoMoney.add(new BigDecimal(detail.getRuKuZongJia()));
            }
        }
        tongJiInfo.setMoney(money);
        tongJiInfo.setTuiHuoMoney(tuiHuoMoney);
        tongJiInfo.setRealMoney(money.subtract(tuiHuoMoney));

        return tongJiInfo;
    }

    private List<GongYingShangTongJiDetail> getTongJiDetail(Long id, String startDay, String endDay) {
        startDay = startDay +" 00:00:00";
        endDay = endDay + " 23:59:59";
        return tongJiService.gongYingShangTongJi(id, startDay, endDay);
    }

    @PostMapping("/export")
    public AjaxResult export(@RequestParam("id") Long id,
                             @RequestParam("startDay") String startDay,
                             @RequestParam("endDay") String endDay,
                             HttpServletResponse response) throws IOException {

        List<GongYingShangTongJiDetail> tongJiDetails = getTongJiDetail(id, startDay, endDay);
        GongYingShangTongJiInfo tongJiInfo = getTongJiInfo(id, startDay, endDay, tongJiDetails);

        List<SheetData> sheetDataList = new ArrayList<>();
        sheetDataList.add(cerateDangKouTongJiSheetData(tongJiInfo));
        sheetDataList.add(createDongKouTongJiDetail(tongJiDetails));
        ExcelExporter.exportToMultiSheetExcel(sheetDataList, response.getOutputStream());

        return AjaxResult.success();
    }

    private SheetData createDongKouTongJiDetail(List<GongYingShangTongJiDetail> tongJiDetails) {
        SheetData tongJiSheet = new SheetData();
        tongJiSheet.setName("明细页");

        List<String> titles = new ArrayList<>();
        titles.add("货品编号");
        titles.add("货品名称");
        titles.add("入库日期");
        titles.add("入库数量");
        titles.add("入库价格");
        titles.add("实际总价");
        titles.add("是否退货");

        tongJiSheet.getData().add(titles);

        for(GongYingShangTongJiDetail detail : tongJiDetails) {
            List<String> dataLine = new ArrayList<>();
            dataLine.add(detail.getHuoPinId());
            dataLine.add(detail.getHuoPinName());
            dataLine.add(detail.getRuKuTime());
            dataLine.add(detail.getRuKuCount());
            dataLine.add(detail.getRuKuPrice());
            dataLine.add(detail.getRuKuZongJia());
            dataLine.add(TuiHuoStatus.ROLLBACK.getCode().equals(detail.getTuiHuo()) ? "已退货": "");

            tongJiSheet.getData().add(dataLine);
        }

        return tongJiSheet;
    }

    private SheetData cerateDangKouTongJiSheetData(GongYingShangTongJiInfo info) {
        SheetData tongJiSheet = new SheetData();
        tongJiSheet.setName("统计页");

        List<String> titles = new ArrayList<>();
        titles.add("供应商号");
        titles.add("供应商名称");
        titles.add("开始日期");
        titles.add("结束日期");
        titles.add("采购总价");
        titles.add("退货总价");
        titles.add("实际总价");

        tongJiSheet.getData().add(titles);

        List<String> dataLine = new ArrayList<>();
        dataLine.add(info.getGongYingShangId()+"");
        dataLine.add(info.getGongYingShangName());
        dataLine.add(info.getStartDay());
        dataLine.add(info.getEndDay());
        dataLine.add(info.getMoney().toPlainString());
        dataLine.add(info.getTuiHuoMoney().toPlainString());
        dataLine.add(info.getRealMoney().toPlainString());

        tongJiSheet.getData().add(dataLine);
        return tongJiSheet;
    }

}
